package com.letcode.study.juc;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Date 2023/3/8 9:06:55
 * @Author luopeng
 */
public class ManyThreadLifeCycleTest {
    public static void main(String[] args) throws InterruptedException {
        //        for (int i = 0; i < 10000; i++) {
        //            TimeUnit.SECONDS.sleep(1);
        //        Thread thread = new Thread(() -> {
        //            byte[] bytes = new byte[1024 * 1024 * 1024];
        //            System.out.println(bytes);
        //            ArrayList bytes1 = new ArrayList<>(Arrays.asList(bytes));
        //            bytes1.clear();
        //            bytes = null;
        //            System.out.println(bytes + "," + bytes1);
        //            System.gc();
        //            System.out.println(new byte[1]);
        //        });
        //        thread.start();
        //        thread.join();
        //        System.out.println("test");

        //        }

        AtomicInteger atomicInteger = new AtomicInteger();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 4, 9000,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10000), Executors.defaultThreadFactory(), (r, executor) -> {
            System.out.println("当前线程被拒绝" + r);
        });
        //        threadPoolExecutor.allowCoreThreadTimeOut(true);
        for (int i = 0; i < 100000; i++) {
            TimeUnit.MILLISECONDS.sleep(10);
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                byte[] bytes = new byte[1024 * 1024 * 1024];
                System.out.println(finalI + "----" + Thread.currentThread().getName() + bytes);
                bytes = null;
//                if (atomicInteger.getAndIncrement() % 2 == 0) {
//                    System.gc();
//                }
            });
        }
    }
}
